﻿package mortal.game.control
{
    import Message.Game.*;
    import Message.Public.*;
    import flash.events.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.view.ranking.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class RankingController extends Controller
    {
        private var _rankingModule:RankingModule;

        public function RankingController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.RankingInfoUpdate, this.rankingInfoUpdateHandler);
            Dispatcher.addEventListener(EventName.ShopMallOpenAndSelect, this.openRankingWin);
            return;
        }// end function

        override protected function initView() : IView
        {
            if (!this._rankingModule)
            {
                this._rankingModule = new RankingModule();
                this._rankingModule.addEventListener(WindowEvent.SHOW, this.onViewShow);
                this._rankingModule.addEventListener(WindowEvent.CLOSE, this.onViewHide);
            }
            return this._rankingModule;
        }// end function

        protected function onViewShow(event:Event) : void
        {
            return;
        }// end function

        protected function onViewHide(event:Event) : void
        {
            return;
        }// end function

        private function rankingInfoUpdateHandler(param1:Object) : void
        {
            var _loc_2:* = param1 as SUpdateMsg;
            cache.ranking.latestVersion = _loc_2.value;
            return;
        }// end function

        private function getRankingListHandler(event:DataEvent) : void
        {
            var _loc_3:Array = null;
            var _loc_2:* = event.data;
            this._rankingModule.switchRankingDisplayType(1);
            if (this._rankingModule.currRankType != _loc_2.type)
            {
                this._rankingModule.rankingDataInfoSpr.clear();
                this._rankingModule.ranking3DdisplaySpr.clear();
                this._rankingModule.currRankType = _loc_2.type as int;
                this._rankingModule.rankingDataInfoSpr.currRankType = _loc_2.type as int;
                this._rankingModule.rankingDataInfoSpr.updataHeadInfo();
                if (_loc_2.type == 404)
                {
                    return;
                }
                _loc_3 = cache.ranking.getRankingDataByType(_loc_2.type);
                if (!_loc_3 || _loc_3[0] && _loc_3[0].version < cache.ranking.latestVersion)
                {
                    GameProxy.ranking.getToplist(_loc_2.type);
                }
                else
                {
                    this._rankingModule.rankingDataInfoSpr.updateAllInfo(_loc_3);
                }
            }
            return;
        }// end function

        private function getRankingFirstInfoHandler(event:DataEvent) : void
        {
            var _loc_2:Array = null;
            this._rankingModule.switchRankingDisplayType(0);
            if (this._rankingModule.currRankType != EToplistType._EToplistTypePlayerOverview)
            {
                this._rankingModule.ranking3DdisplaySpr.clear();
                this._rankingModule.currRankType = EToplistType._EToplistTypePlayerOverview;
                _loc_2 = cache.ranking.getRankingDataByType(EToplistType._EToplistTypePlayerOverview);
                if (!_loc_2 || _loc_2[0] && _loc_2[0].version < cache.ranking.latestVersion)
                {
                    GameProxy.ranking.getToplist(EToplistType._EToplistTypePlayerOverview);
                }
                else
                {
                    this._rankingModule.rankingFirstInfoSpr.data = _loc_2;
                }
            }
            return;
        }// end function

        private function getRankingListSuccHandler(param1:Object) : void
        {
            var _loc_2:Array = null;
            if (param1)
            {
                _loc_2 = param1.rankingList;
                if (_loc_2.length > 0)
                {
                    cache.ranking.saveRankingData(_loc_2[0].toplistType, _loc_2);
                    if (_loc_2[0].toplistType > 0 && this._rankingModule.rankingDataInfoSpr)
                    {
                        this._rankingModule.rankingDataInfoSpr.updateAllInfo(_loc_2);
                    }
                    else if (_loc_2[0].toplistType == 0 && this._rankingModule.rankingFirstInfoSpr)
                    {
                        this._rankingModule.rankingFirstInfoSpr.data = _loc_2;
                    }
                }
            }
            return;
        }// end function

        private function openRankingWin(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            if (_loc_2 == "whosyourdaddy")
            {
                view.show();
            }
            return;
        }// end function

    }
}
